﻿using CampWeek5__Book_Project_.Adapters.Adapters;
using CampWeek5__Book_Project_.Data;
using CampWeek5__Book_Project_.Data.Model;
using CampWeek5__Book_Project_.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CampWeek5__Book_Project_.Controllers
{
    [Authorize(Roles = Roles.ADMIN)]
    public class AdminController : Controller
    {
        private BookAdapter _adapter = new BookAdapter();
        private AuthorAdapter _adapterAuthor = new AuthorAdapter();

        // GET: Admin
        public ActionResult AdminIndex()
        {
            IndexVM model = _adapter.GetIndex();
            return View(model);
        }

        //Serch box
        public ActionResult AdminResultsPage()
        {
            return View();
        }

        [HttpGet]
        public ActionResult AdminResultsPage(string search)
        {
            IndexVM model = _adapter.SearchBooks(search);
            return View(model);
        }

        #region Books CRUD

        //create book - default
        public ActionResult CreateBook(int id)
        {
            BookVM model = new BookVM();
            model.AuthorId = id;
            model.Tags = new List<string>();
            return View(model);
        }

        //create book - POST
        [HttpPost]
        public ActionResult CreateBook(BookVM book)
        {
            if (ModelState.IsValid)
            {
                _adapter.CreateBook(book);
                return RedirectToAction("AdminIndex");
            }
            return View(book);
        }

        //delete book
        public ActionResult AdminDeleteBook(int Id)
        {
            _adapter.DeleteBook(Id);
            return RedirectToAction("AdminIndex");
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////
        //edit book - default
        public ActionResult AdminEditBook(int Id)
        {
            BookVM model = new BookVM();

            _adapter.AdminEditGetBook(Id, model);


            //model.Tags = new List<string>();
            return View("CreateBook", model);
        }

        //edit book - POST
        [HttpPost]
        public ActionResult AdminEditBook(BookVM book)
        {
            if (ModelState.IsValid)
            {
                _adapter.AdminEditBook(book);
                return RedirectToAction("AdminIndex");
            }
            return View(book);
        }

        #endregion Books CRUD

        #region Authors CRUD

        //create author - default
        public ActionResult CreateAuthor()
        {
            AuthorVM model = new AuthorVM();
            model.Tags = new List<string>();
            model.DateOfDeath = null;
            return View(model);
        }

        //create author - POST
        [HttpPost]
        public ActionResult CreateAuthor(AuthorVM author)
        {
            if (ModelState.IsValid)
            {
                AuthorVM model = _adapterAuthor.CreateAuthor(author);
                }
                return RedirectToAction("AdminIndex");
            }

        // delete author
        
        public ActionResult AdminDeleteAuthor(int Id)
        {
            _adapterAuthor.AdminDeleteAuthor(Id);
            return RedirectToAction("AdminIndex");
        }

        //edit author - default
        public ActionResult AdminEditAuthor(int Id)
        {
         AuthorVM model= _adapterAuthor.AdminGetAuthor(Id);
            return View("CreateAuthor", model);
        }

        //edit author - POST
        //Still has unresolved errors to get fixed
        [HttpPost]
        public ActionResult AdminEditAuthor(AuthorVM model)
        {
            if (ModelState.IsValid)
            {
               _adapterAuthor.AdminEditAuthor(model);
               return RedirectToAction("AdminIndex");
            } 



            return View("CreateAuthor", model);
        }

        

        #endregion Authors CRUD

        public ActionResult DeleteRating(int id)
        {
            _adapter.DeleteRating(id);
            return RedirectToAction("Index");
        }
    }
}